#include <iostream>
using std::cout;
using std::endl;

#include "client/client.h"

#include <Poco/File.h>
using Poco::File;

#include <Poco/StringTokenizer.h>
using Poco::StringTokenizer;

#include <iostream>
using namespace std;

#include <map>

Client client;

typedef void (*CLI)(string const&, istream &, ostream&, ostream&);

void join(string const& s, istream& in, ostream& out, ostream& err)
{
	client.connect("cecilia");
	out << "runClient :: Joined ring" << endl;
}

void leave(string const& s, istream& in, ostream& out, ostream& err)
{
	client.disconnect();
}

void put(string const& s, istream& in, ostream& out, ostream& err)
{
	vector<File> files;
	out << "filename: ";
	while (true) {
		string filename;
		in >> filename;

		if (filename == "::")
			break;

		files.push_back(File(filename));
	}

	client.uploadFiles(files);
}

void get(string const& s, istream& in, ostream& out, ostream& err)
{
	vector<File> files;
	out << "filename: ";
	while (true) {
		string filename;
		in >> filename;

		if (filename == "::")
			break;

		files.push_back(File(filename));
	}

	client.downloadFiles(files);
}

int main(int argc, char** argv)
{
	map< string, CLI> commands;
	commands["join"] = &join;
	commands["leave"] = &leave;
	commands["put"] = &put;
	commands["get"] = &get;

	while( true ){
		string command;
		cout << "> ";
		cin >> command;

		if (command == "exit") {
			break;
		} else if (command == "help") {
			for (map<string,CLI>::iterator it = commands.begin(); it != commands.end(); ++it)
				cout << it->first << " ";
			cout << "exit help" << endl;
		}

		StringTokenizer tok (command, " ", StringTokenizer::TOK_IGNORE_EMPTY);
		CLI cmd = commands[tok[0]];

		if(cmd != 0){
			cmd(command, cin, cout, cerr);
		}
	}
}
